/**
 * Project: springyounger.lottery.web
 * 
 * File Created at 2010-3-27
 * $Id$
 * 
 * Copyright 2009 Zjut Croporation Limited.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * Zjut Edu. ("Confidential Information").  You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with Zjut Edu.
 */
package com.springyouner.lottery.admin;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
import org.springframework.web.util.WebUtils;

import com.springyouner.lottery.framework.UserSession;
import com.springyouner.lottery.tools.LotteryUtil;
import com.springyounger.lottery.dal.dataobject.LotteryPhase;
import com.springyounger.lottery.dal.dataobject.LotteryPhaseQuestion;
import com.springyounger.lottery.dal.dataobject.LotteryQuestionResult;
import com.springyounger.lottery.dal.dataobject.LotteryQuota;
import com.springyounger.lottery.dal.dataobject.User;
import com.springyounger.lottery.service.user.PhaseService;
import com.springyounger.lottery.service.user.QuestionService;

/**
 * 初始化题目
 * 
 * @author springyounger
 */
public class InitQuestionController implements Controller {

	private static final Logger logger = LoggerFactory
			.getLogger(InitQuestionController.class);
	private PhaseService phaseService;
	private QuestionService questionService;

	@SuppressWarnings("unchecked")
	public ModelAndView handleRequest(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String flag = ""; // 错误标志
		Map model = new HashMap();
		int avaliableCount;
		int usedCount;
		UserSession userSession = (UserSession) WebUtils.getSessionAttribute(
				request, "userSession");
		User user = userSession.getUser();

		// 当前时间
		Date today = LotteryUtil.dateOnlyExt(LotteryUtil.getToday());
		LotteryPhaseQuestion phaseQuestion = null;
		List<LotteryQuestionResult> questionResult = new ArrayList<LotteryQuestionResult>();

		String phaseId = request.getParameter("phaseId");
		if (LotteryUtil.isEmpty(phaseId)) {
			logger.info("活动id为空");
			flag = "lotteryNull";
			model.put("flag", flag);
			model.put("phaseId", phaseId);
			return new ModelAndView("user/lotteryError", model);
		}
		int pId = Integer.parseInt(phaseId);
		LotteryPhase lotteryPhase = phaseService.getPhaseById(pId);

		if (null == lotteryPhase) {
			// 活动为空
			logger.info("活动不存在!");
			flag = "lotteryNull";
			model.put("flag", flag);
			model.put("phaseId", phaseId);
			return new ModelAndView("user/lotteryError", model);
		}
		// 查询抽奖配额
		LotteryQuota lotteryQuota = new LotteryQuota();
		lotteryQuota.setPhaseId(lotteryPhase.getPhaseId());
		lotteryQuota.setUserId(user.getUserid());
		lotteryQuota.setQuotaType(LotteryQuota.LOTTERY_DAILY);
		lotteryQuota.setQuotaDate(LotteryUtil.getToday());

		LotteryQuota quota = phaseService.getLotteryQuota(lotteryQuota);

		if (null == quota) {
			// 首次答题
			logger.info("第一次答题");
			flag = "firstTime";
			phaseQuestion = questionService.getPhaseQuestion(pId, today, "1",
					"0");
			avaliableCount = lotteryPhase.getDailyQuestion();
			// 已使用的题数
			usedCount = 0;
		} else {
			// 非首次答题
			logger.info("非首次答题");
			avaliableCount = quota.getAvaliableCount();
			if (avaliableCount <= 0) {
				// 配额已经用完
				flag = "quotaZero";
				model.put("flag", flag);
				model.put("phaseId", phaseId);
				return new ModelAndView("user/lotteryError", model);
			}
			usedCount = quota.getQuotaAmount() - avaliableCount;
			String publishSn = String.valueOf(usedCount + 1);
			phaseQuestion = questionService.getPhaseQuestion(pId, today,
					publishSn, "0");
		}
		if (null == phaseQuestion) {
			// 没有对应的题目
			logger.info("对应的题目为空");
			flag = "questionNull";
			model.put("flag", flag);
			model.put("phaseId", phaseId);
			return new ModelAndView("user/lotteryError", model);
		}
		// 获得对应题目的答案
		questionResult = questionService.getQuestinResult(phaseQuestion
				.getPhaseQuestionId());

		model.put("flag", flag);
		model.put("avaliableCount", String.valueOf(avaliableCount));
		model.put("usedCount", String.valueOf(usedCount));
		model.put("question", phaseQuestion);
		model.put("questionResult", questionResult);
		model.put("phaseId", phaseId);
		return new ModelAndView("user/initQuestion", model);
	}

	/**
	 * @param phaseService
	 *            the phaseService to set
	 */
	public void setPhaseService(PhaseService phaseService) {
		this.phaseService = phaseService;
	}

	/**
	 * @param questionService
	 *            the questionService to set
	 */
	public void setQuestionService(QuestionService questionService) {
		this.questionService = questionService;
	}

}
